import { useRoutes, Outlet, Navigate, RouteObject } from "react-router-dom";
import React, { lazy, ReactElement, Suspense } from "react";
import Loading from "@/components/Loading";
import AuthComponent from "@/components/authComponent";
import Index from "@/pages/Index";
import {
    HomeOutlined,
    MedicineBoxOutlined,
    DatabaseOutlined,
    UserOutlined,
    UnorderedListOutlined,
    UserAddOutlined
} from '@ant-design/icons';
const lazyLoad = (path: string): ReactElement => {
    const Comp = lazy(() => import("@/pages/" + path))
    return (
        <Suspense fallback={<Loading />}>
            <Comp />
        </Suspense>
    )
}
export interface IRoutes extends RouteObject {
    title?: string,
    children?: IRoutes[],
    icon?: React.ReactElement,
    isHide?: boolean
}
export type TRoutes = IRoutes[];
const routes: TRoutes = [
    {
        path: "/",
        element: <AuthComponent><Index /></AuthComponent>,
        children: [
            {
                title: "主页",
                path: "/",
                icon: <HomeOutlined />,
                element: lazyLoad("home")

            },
            {
                // 数据管理
                path: "/cmn",
                title: "数据管理",
                icon: <DatabaseOutlined />,
                element: <Outlet />,
                children: [
                    {
                        path: "/cmn",
                        isHide: true,
                        element: <Navigate to="/cmn/dict" />
                    },
                    {
                        // 数据字典
                        path: "/cmn/dict",
                        title: "数据字典",
                        element: lazyLoad("cmn/dict")
                    }
                ]
            },
            {
                // 医院管理
                title: "医院管理",
                path: "/hospital",
                icon: <MedicineBoxOutlined />,
                element: <Outlet />,
                children: [
                    {
                        // index:true,
                        isHide: true,
                        path: "/hospital",
                        element: <Navigate to="/hospital/hospitalSet" />
                    },
                    {
                        // 医院设置
                        title: "医院设置",
                        path: "/hospital/hospitalSet",
                        element: lazyLoad("hospital/hospitalSet"),
                    },
                    {
                        // 医院列表
                        title: "医院列表",
                        path: "/hospital/hospitalList",
                        element: lazyLoad("hospital/hospitalList"),
                    }, {
                        // 如果为true则不在侧边栏显示。
                        isHide: true,
                        title: "医院详情页",
                        path: "/hospital/show/:id",
                        element: lazyLoad("hospital/show"),
                    }
                    , {
                        // 如果为true则不在侧边栏显示。
                        isHide: true,
                        title: "医院排班",
                        path: "/hospital/department/:hoscode",
                        element: lazyLoad("hospital/department"),
                    },
                ]
            },
            {
                path: "/user",
                title: "用户管理",
                icon: <UserOutlined />,
                element: <Outlet />,
                children: [
                    {
                        path: "/user",
                        isHide: true,
                        element: <Navigate to="/user/userList" />
                    },
                    {
                        path: "/user/userList",
                        title: "用户列表",
                        element: lazyLoad("user/userList")
                    }, {
                        // 如果为true则不在侧边栏显示。
                        isHide: true,
                        title: "个人详情页",
                        path: "/user/show/:id",
                        element: lazyLoad("user/show"),
                    }
                ]
            }, {
                path: "/order",
                title: "订单管理",
                icon: <UnorderedListOutlined />,
                element: <Outlet />,
                children: [
                    {
                        path: "/order",
                        isHide: true,
                        element: <Navigate to="/order/orderList" />
                    },
                    {
                        path: "/order/orderList",
                        title: "订单列表",
                        element: lazyLoad("order/orderList")
                    }, {
                        // 如果为true则不在侧边栏显示。
                        isHide: true,
                        title: "订单详情",
                        path: "/order/show/:id",
                        element: lazyLoad("order/show"),
                    }
                ]
            },
            {
                path: "/product",
                title: "商品管理",
                icon: <UserAddOutlined/>,
                element: <Outlet />,
                children: [
                    {
                        path: "/product",
                        isHide: true,
                        element: <Navigate to="/product/product" />
                    },
                    {
                        path: "/product/trademark",
                        title: "品牌管理",
                        element:lazyLoad("product/trademark/trademarkList")
                    }, 
                    {
                        path: "/product/attr/",
                        title: "平台属性管理",
                        element:lazyLoad("product/attr/attrList")
                    }, 
                ]
            },
           
            
        ]
    },
    {
      
    },
    {
        path: "/Login",
        element: lazyLoad("Login")
    },
    {
        path: "*",
        element: lazyLoad("NotFound")
    }
]
//  侧边栏需要的路由信息
export function findSiderRouter(): IRoutes[] {
    const info: IRoutes = routes.find(v => v.path === "/") as IRoutes;
    return info.children as IRoutes[];
}
// 整体的路由配置信息
export default function RenderRouter(): ReactElement {
    return useRoutes(routes) as ReactElement;
}